package com.javabasic.algorithm.leetcode;

/**
 * @Author xiongmin
 * @Description //TODO
 * @Date 2020/2/3 11:43
 * @Version 1.0
 **/
public class SpiralMatrixII {
    /**
     * 模拟过程 - 蛇形填数
     * @param n
     * @return
     */
    public int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];
        int i = 0, j = 0, k = 0;
        result[i][j] = ++k;
        int len = n*n;
        while (k <= len) {
            while (j + 1 < n && result[i][j + 1] == 0 && k + 1 <= len) {
                result[i][++j] = ++k;
            }
            while (i + 1 < n && result[i + 1][j] == 0 && k + 1 <= len) {
                result[++i][j] = ++k;
            }
            while (j - 1 >= 0 && result[i][j - 1] == 0 && k + 1 <= len) {
                result[i][--j] = ++k;
            }
            while (i - 1 >= 0 && result[i - 1][j] == 0 && k + 1 <= len) {
                result[--i][j] = ++k;
            }
            System.out.println(k);
        }
        return result;
    }
}
